home *** CD-ROM | disk | FTP | other *** search
/ World of Sound / World of Sound.iso / utils / modplayers / tracker / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  12.5 KB  |  577 lines

  1. /* commands.c */
  2.  
  3. /* $Id: commands.c,v 3.14 1993/11/17 15:31:16 espie Exp espie $
  4.  * $Log: commands.c,v $
  5.  * Revision 3.14  1993/11/17  15:31:16  espie
  6.  * play_note instead of ch->mode.
  7.  *
  8.  * Revision 3.13  1993/08/04  11:34:33  espie
  9.  * *** empty log message ***
  10.  *
  11.  * Revision 3.12  1993/07/18  10:39:44  espie
  12.  * Cleaned up.
  13.  *
  14.  * Revision 3.11  1993/07/17  22:23:41  espie
  15.  * Fixed bug with bad loops.
  16.  *
  17.  * Revision 3.9  1993/05/09  14:06:03  espie
  18.  * Modified the way set_speed works.
  19.  *
  20.  * Revision 3.8  1993/04/28  20:13:13  espie
  21.  * Very small bug with volume (Lawrence).
  22.  *
  23.  * Revision 3.7  1993/01/15  14:00:28  espie
  24.  * Added bg/fg test.
  25.  *
  26.  * Revision 3.6  1992/11/27  10:29:00  espie
  27.  * General cleanup
  28.  *
  29.  * Revision 3.5  1992/11/24  10:51:19  espie
  30.  * More precise vibrato table.
  31.  *
  32.  * Revision 3.4  1992/11/23  10:12:23  espie
  33.  * *** empty log message ***
  34.  *
  35.  * Revision 3.3  1992/11/22  17:20:01  espie
  36.  * Simplified delay_pattern.
  37.  *
  38.  * Revision 3.2  1992/11/20  14:53:32  espie
  39.  * Added finetune.
  40.  *
  41.  * Revision 3.1  1992/11/19  20:44:47  espie
  42.  * Protracker commands.
  43.  *
  44.  * Revision 3.0  1992/11/18  16:08:05  espie
  45.  * New release.
  46.  *
  47.  * Revision 2.12  1992/11/13  13:24:24  espie
  48.  * Added some extended commands: E12AB, and some.
  49.  * now use set_volume in audio.c. All the device-dependent operation
  50.  * is there.
  51.  * Defensive programming: check the range of each note
  52.  * for arpeggio setup.
  53.  * Structured part of the code, especially replay ``automaton''
  54.  * and setting up of effects.
  55.  *
  56.  * Revision 1.9  1991/11/17  17:09:53  espie
  57.  * Added missing prototypes.
  58.  * Dynamic oversample and frequency.
  59.  * Added arpeggio.
  60.  * Fixed up vibrato depth.
  61.  * Added vibslide and portaslide.
  62.  * Added command 9.
  63.  */
  64.  
  65. #include <stdio.h>
  66.  
  67. #include "defs.h"
  68. #include "extern.h"
  69. #include "channel.h"
  70. #include "song.h"
  71.      
  72. LOCAL char *id = "$Id: commands.c,v 3.14 1993/11/17 15:31:16 espie Exp espie $";
  73.  
  74. /* sine table for the vibrato effect (obtained through build.c) */
  75.  
  76. int vibrato_table[64] = 
  77.     {
  78.     0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  79.     510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  80.     -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  81.     -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  82.     -195,-148,-99,-49
  83.     };
  84.  
  85. /***
  86.  *
  87.  *
  88.  *  setting up effects/doing effects.
  89.  *  The set_xxx gets called while parsing the effect,
  90.  *  the do_xxx gets called each tick, and update the
  91.  *  sound parameters while playing it.
  92.  *
  93.  *
  94.  ***/
  95.  
  96.  
  97. void do_nothing(ch)
  98. struct channel *ch;
  99.     {
  100.     }
  101.  
  102. void set_nothing(a, ch)
  103. struct automaton *a;
  104. struct channel *ch;
  105.     {
  106.     }
  107.  
  108. /* slide pitch (up or down) */
  109. void do_slide(ch)
  110. struct channel *ch;
  111.     {
  112.     ch->pitch += ch->slide;
  113.     ch->pitch = MIN(ch->pitch, MAX_PITCH);
  114.     ch->pitch = MAX(ch->pitch, MIN_PITCH);
  115.     set_current_pitch(ch, ch->pitch);
  116.     }
  117.  
  118. void set_upslide(a, ch)
  119. struct automaton *a;
  120. struct channel *ch;
  121.     {
  122.     ch->adjust = do_slide;
  123.     if (a->para)
  124.         ch->slide = a->para;
  125.     }
  126.  
  127. void set_downslide(a, ch)
  128. struct automaton *a;
  129. struct channel *ch;
  130.     {
  131.     ch->adjust = do_slide;
  132.     if (a->para)
  133.         ch->slide = -a->para;
  134.     }
  135.  
  136. /* modulating the pitch with vibrato */
  137. void do_vibrato(ch)
  138. struct channel *ch;
  139.     {
  140.     int offset;
  141.  
  142.         /* this is no longer a literal transcription of the pt
  143.          * code. I have rescaled the vibrato table.
  144.          */
  145.     ch->viboffset += ch->vibrate;
  146.     ch->viboffset &= 63;
  147.         /* please don't use logical shift on signed values */
  148.     offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  149.         /* temporary update of only the step value,
  150.          * note that we do not change the saved pitch.
  151.          */
  152.     set_current_pitch(ch, ch->pitch + offset);
  153.     }
  154.  
  155. void set_vibrato(a, ch)
  156. struct automaton *a;
  157. struct channel *ch;
  158.     {
  159.     ch->adjust = do_vibrato;
  160.     if (HI(a->para))
  161.         ch->vibrate = HI(a->para);
  162.     if (LOW(a->para))
  163.         ch->vibdepth = LOW(a->para);
  164.     }
  165.  
  166. /* arpeggio looks a bit like chords: we alternate between two
  167.  * or three notes very fast.
  168.  */
  169. void do_arpeggio(ch)
  170. struct channel *ch;
  171.     {
  172.     if (++ch->arpindex >= MAX_ARP)
  173.         ch->arpindex =0;
  174.     set_current_pitch(ch, ch->arp[ch->arpindex]);
  175.     }
  176.  
  177. void set_arpeggio(a, ch)
  178. struct automaton *a;
  179. struct channel *ch;
  180.     {
  181.         /* arpeggio can be installed relative to the
  182.          * previous note, so we have to check that there
  183.          * actually is a current(previous) note
  184.          */
  185.     if (ch->note == NO_NOTE)
  186.         {
  187.         fprintf(stderr,
  188.             "No note present for arpeggio");
  189.         error = FAULT;
  190.         }
  191.     else
  192.         {
  193.         int note;
  194.  
  195.         ch->arp[0] = pitch_table[ch->note][ch->finetune];
  196.         note = ch->note + HI(a->para);
  197.         if (note < NUMBER_NOTES)
  198.             ch->arp[1] = pitch_table[note][ch->finetune];
  199.         else
  200.             {
  201.             fprintf(stderr,
  202.                 "Arpeggio note out of range");
  203.             error = FAULT;
  204.             }
  205.         note = ch->note + LOW(a->para);
  206.         if (note < NUMBER_NOTES)
  207.             ch->arp[2] = pitch_table[note][ch->finetune];
  208.         else
  209.             {
  210.             fprintf(stderr,
  211.                 "Arpeggio note out of range");
  212.             error = FAULT;
  213.             }
  214.         ch->arpindex = 0;
  215.         ch->adjust = do_arpeggio;
  216.         }
  217.     }
  218.  
  219. /* volume slide. Mostly used to simulate waveform control.
  220.  * (attack/decay/sustain).
  221.  */
  222. void do_slidevol(ch)
  223. struct channel *ch;
  224.     {
  225.     set_current_volume(ch, ch->volume + ch->volumerate);
  226.     }
  227.  
  228. /* note that volumeslide does not have a ``take default''
  229.  * behavior. If para is 0, this is truly a 0 volumeslide.
  230.  * Issue: is the test really necessary ? Can't we do
  231.  * a HI(para) - LOW(para). Answer: protracker does not.
  232.  */
  233. void parse_slidevol(ch, para)
  234. struct channel *ch;
  235. int para;
  236.     {
  237.     if (LOW(para))
  238.         ch->volumerate = -LOW(para);
  239.     else
  240.         ch->volumerate = HI(para);
  241.     }
  242.  
  243. void set_slidevol(a, ch)
  244. struct automaton *a;
  245. struct channel *ch;
  246.     {
  247.     ch->adjust = do_slidevol;
  248.     parse_slidevol(ch, a->para);
  249.     }
  250.  
  251. /* portamento: gets from a given pitch to another.
  252.  * We can simplify the routine by cutting it in
  253.  * a pitch up and pitch down part while setting up
  254.  * the effect.
  255.  */
  256. void do_portamento(ch)
  257. struct channel *ch;
  258.     {
  259.     if (ch->pitch < ch->pitchgoal)
  260.         {
  261.         ch->pitch += ch->pitchrate;
  262.         ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  263.         }
  264.     else if (ch->pitch > ch->pitchgoal)
  265.         {
  266.         ch->pitch -= ch->pitchrate;
  267.         ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  268.         }
  269.         /* if we want to implement funk glissando, we need a change right
  270.          * there
  271.          */
  272.     set_current_pitch(ch, ch->pitch);
  273.     }
  274.  
  275. /* if para and pitch are 0, this is obviously a continuation
  276.  * of the previous portamento.
  277.  */
  278. void set_portamento(a, ch)
  279. struct automaton *a;
  280. struct channel *ch;
  281.     {
  282.     ch->adjust = do_portamento;
  283.     if (a->para)
  284.         ch->pitchrate = a->para;
  285.     if (a->pitch)
  286.         ch->pitchgoal = a->pitch;
  287.     }
  288.  
  289. /*
  290.  * combined commands.
  291.  */
  292. void do_portaslide(ch)
  293. struct channel *ch;
  294.     {
  295.     do_portamento(ch);
  296.     do_slidevol(ch);
  297.     }
  298.  
  299. void set_portaslide(a, ch)
  300. struct automaton *a;
  301. struct channel *ch;
  302.     {
  303.     ch->adjust = do_portaslide;
  304.     if (a->pitch)
  305.         ch->pitchgoal = a->pitch;
  306.     parse_slidevol(ch, a->para);
  307.     }
  308.  
  309. void do_vibratoslide(ch)
  310. struct channel *ch;
  311.     {
  312.     do_vibrato(ch);
  313.     do_slidevol(ch);
  314.     }
  315.  
  316. void set_vibratoslide(a, ch)
  317. struct automaton *a;
  318. struct channel *ch;
  319.     {
  320.     ch->adjust = do_vibratoslide;
  321.     parse_slidevol(ch, a->para);
  322.     }
  323.  
  324. /***
  325.  *
  326.  *  effects that just need a setup part
  327.  *
  328.  ***/
  329.  
  330. /* IMPORTANT: because of the special nature of
  331.  * the player, we can't process each effect independently,
  332.  * we have to merge effects from the four channel before
  333.  * doing anything about it. For instance, there can be 
  334.  * several speed change in the same note.
  335.  */
  336. void set_speed(a, ch)
  337. struct automaton *a;
  338. struct channel *ch;
  339.     {
  340.     if (a->para >= 32)
  341.         {
  342.         a->new_finespeed = a->para;
  343.         a->do_stuff |= SET_FINESPEED;
  344.         }
  345.     else if (a->para)
  346.         {
  347.         a->new_speed = a->para;
  348.         a->do_stuff |= SET_SPEED;
  349.         }
  350.     }
  351.  
  352. void set_skip(a, ch)
  353. struct automaton *a;
  354. struct channel *ch;
  355.     {
  356.         /* BCD decoding in read.c */
  357.     a->new_note = a->para;
  358.     a->do_stuff |= SET_SKIP;
  359.     }
  360.  
  361. void set_fastskip(a, ch)
  362. struct automaton *a;
  363. struct channel *ch;
  364.     {
  365.     a->new_pattern = a->para;
  366.     a->do_stuff |= SET_FASTSKIP;
  367.     }
  368.  
  369. /* immediate effect: starts the sample somewhere
  370.  * off the start.
  371.  */
  372. void set_offset(a, ch)
  373. struct automaton *a;
  374. struct channel *ch;
  375.     {
  376.     set_position(ch, a->para * 256);
  377.     }
  378.  
  379. /* change the volume of the current channel.
  380.  * Is effective until there is a new set_volume,
  381.  * slide_volume, or an instrument is reloaded 
  382.  * explicitly by giving its number. Obviously, if
  383.  * you load an instrument and do a set_volume in the
  384.  * same note, the set_volume will take precedence.
  385.  */
  386. void set_volume(a, ch)
  387. struct automaton *a;
  388. struct channel *ch;
  389.     {
  390.     set_current_volume(ch, a->para);
  391.     }
  392.  
  393.  
  394.  
  395. /***
  396.  *
  397.  *    EXTENDED COMMANDS
  398.  *
  399.  ***/
  400.  
  401. /* extended command: retrig note at a fast pace
  402.  */
  403. void do_retrig(ch)
  404. struct channel *ch;
  405.     {
  406.     if (--ch->current <= 0)
  407.         {
  408.         reset_note(ch, ch->note, ch->pitch);
  409.         ch->current = ch->retrig;
  410.         }
  411.     }
  412.  
  413. void set_retrig(a, ch)
  414. struct automaton *a;
  415. struct channel *ch;
  416.     {
  417.     ch->retrig = a->para;
  418.     ch->current = ch->retrig;
  419.     ch->adjust = do_retrig;
  420.     }
  421.  
  422. /* extended command: start note after a small
  423.  * delay
  424.  */
  425. void do_latestart(ch)
  426. struct channel *ch;
  427.     {
  428.     if (--ch->current <= 0)
  429.         {
  430.         reset_note(ch, ch->note, ch->pitch);
  431.         ch->adjust = do_nothing;
  432.         }
  433.     }
  434.  
  435. void set_late_start(a, ch)
  436. struct automaton *a;
  437. struct channel *ch;
  438.     {
  439.     play_note(ch->audio, NULL, 0);
  440.     ch->current = a->para;
  441.     ch->adjust = do_latestart;
  442.     }
  443.  
  444. /* extended command: cut note after some time.
  445.  * Note we only kill the volume, as protracker does...
  446.  */
  447. void do_cut(ch)
  448. struct channel *ch;
  449.     {
  450.     if (ch->retrig)
  451.         {
  452.         if (--ch->retrig == 0)
  453.             set_current_volume(ch, 0);
  454.         }
  455.     }
  456.  
  457. void set_note_cut(a, ch)
  458. struct automaton *a;
  459. struct channel *ch;
  460.     {
  461.     ch->retrig = a->para;
  462.     ch->adjust = do_cut;
  463.     }
  464.  
  465.  
  466. void set_smooth_up(a, ch)
  467. struct automaton *a;
  468. struct channel *ch;
  469.     {
  470.     ch->pitch += a->para;
  471.     ch->pitch = MIN(ch->pitch, MAX_PITCH);
  472.     ch->pitch = MAX(ch->pitch, MIN_PITCH);
  473.     set_current_pitch(ch, ch->pitch);
  474.     }
  475.  
  476. void set_smooth_down(a, ch)
  477. struct automaton *a;
  478. struct channel *ch;
  479.     {
  480.     ch->pitch -= a->para;
  481.     ch->pitch = MIN(ch->pitch, MAX_PITCH);
  482.     ch->pitch = MAX(ch->pitch, MIN_PITCH);
  483.     set_current_pitch(ch, ch->pitch);
  484.     }
  485.  
  486. void set_change_finetune(a, ch)
  487. struct automaton *a;
  488. struct channel *ch;
  489.     {
  490.     ch->finetune = a->para;
  491.     }
  492.  
  493.  
  494. void set_loop(a, ch)
  495. struct automaton *a;
  496. struct channel *ch;
  497.     {
  498.         /* Note: the current implementation of protracker
  499.          * does not allow for a jump from pattern to pattern,
  500.          * even though it looks like a logical extension to the current 
  501.          * format.
  502.          */
  503.     if (a->para == 0) 
  504.         a->loop_note_num = a->note_num;
  505.     else
  506.         {
  507.         if (a->loop_counter == 0)
  508.             a->loop_counter = a->para + 1;
  509.         /* We have to defer the actual count-down and note jump
  510.          * to automaton.c, because some modules include several
  511.          * loops on the same measure, which is a bit confusing
  512.          * (see don't you want me)
  513.          */
  514.         a->do_stuff |= JUMP_PATTERN;
  515.         }
  516.     }
  517.  
  518. void set_smooth_upvolume(a, ch)
  519. struct automaton *a;
  520. struct channel *ch;
  521.     {
  522.     set_current_volume(ch, ch->volume + a->para);
  523.     }
  524.  
  525. void set_smooth_downvolume(a, ch)
  526. struct automaton *a;
  527. struct channel *ch;
  528.     {
  529.     set_current_volume(ch, ch->volume - a->para);
  530.     }
  531.  
  532.  
  533. void set_delay_pattern(a, ch)
  534. struct automaton *a;
  535. struct channel *ch;
  536.     {
  537.     a->counter -= (a->para + 1) * a->speed;
  538.     a->do_stuff |= DELAY_PATTERN;
  539.     }
  540.  
  541.  
  542.  
  543. /* Initialize the whole effect table */
  544.  
  545. void init_effects(table)
  546. void (*table[])();
  547.     {
  548.     int i;
  549.  
  550.     for (i = 0; i < NUMBER_EFFECTS; i++)
  551.         table[i] = set_nothing;
  552.     table[EFF_ARPEGGIO] = set_arpeggio;
  553.     table[EFF_SPEED] = set_speed;
  554.     table[EFF_SKIP] = set_skip;
  555.     table[EFF_FF] = set_fastskip;
  556.     table[EFF_VOLUME] = set_volume;
  557.     table[EFF_VOLSLIDE] = set_slidevol;
  558.     table[EFF_OFFSET] = set_offset;
  559.     table[EFF_PORTA] = set_portamento;
  560.     table[EFF_PORTASLIDE] = set_portaslide;
  561.     table[EFF_UP] = set_upslide;
  562.     table[EFF_DOWN] = set_downslide;
  563.     table[EFF_VIBRATO] = set_vibrato;
  564.     table[EFF_VIBSLIDE] = set_vibratoslide;
  565.     table[EFF_SMOOTH_UP] = set_smooth_up;
  566.     table[EFF_SMOOTH_DOWN] = set_smooth_down;
  567.     table[EFF_CHG_FTUNE] = set_change_finetune;
  568.     table[EFF_LOOP] = set_loop;
  569.     table[EFF_RETRIG] = set_retrig;
  570.     table[EFF_S_UPVOL] = set_smooth_upvolume;
  571.     table[EFF_S_DOWNVOL] = set_smooth_downvolume;
  572.     table[EFF_NOTECUT] = set_note_cut;
  573.     table[EFF_LATESTART] = set_late_start;
  574.     table[EFF_DELAY] = set_delay_pattern;
  575.     }
  576.  
  577.